<?php


	class FTAFaqs extends FTAObject {
		
		var $cmsDocument;
		var $renderFaqsFunction;
		var $allFAQCategories;
		var $categoryIdentifier;
		var $FAQsCategory;
		var $renderFAQsCategoryFunction;
		
		
		function __construct() {
			parent::__construct();
			
			global $db;	
			$this->cmsDocument = new CmsDocument($db);
						
		}
		
//		
		function getFaqs($pageSize = null, $pageNumber = null, $category = null, $sort = null, $direction = null){
			//this gets Faqs and filters based on the category if there is one set.. 
			$this->cmsDocument->flush();
			$this->cmsDocument->setActiveDocuments();
			$this->cmsDocument->addDocumentType('docFaq');
			
			if(!is_null($category)){
				$this->cmsDocument->addConstraint("categorycode", $category);
			} elseif($this->FAQsCategory){
				$this->cmsDocument->addConstraint('categorycode',$this->FAQsCategory->documentcode,'=');
			}
			if(!is_null($sort)){
				if(!is_null($direction)){
					$this->cmsDocument->addSort($sort, $direction);
				} else {
					$this->cmsDocument->addSort($sort);
				}
			} else {
				$this->cmsDocument->addSort("<<md.rank>>");
			}
			
			$this->cmsDocument->setPageSize($pageSize);
			$this->cmsDocument->setPageNumber($pageNumber);
			
			$this->cmsDocument->resetIterator();
			
			$allDocuments = $this->cmsDocument->getAllDocuments();
			
			if (!$allDocuments)
				$allDocuments = array();
				
			return $allDocuments;
						
		}
		
		function getFAQCategories(){
			if (is_null($this->allFAQCategories)){
			
				$this->cmsDocument->flush();
				$this->cmsDocument->setActiveDocuments();
				$this->cmsDocument->addDocumentType('docFaqCat');
				$this->cmsDocument->addSort('<<md.rank>>');
				$this->cmsDocument->resetIterator();
			
				$this->allFAQCategories = $this->cmsDocument->getAllDocuments();
				
				if (!is_array($this->allFAQCategories))
					$this->allFAQCategories = array();

			}
			
			return $this->allFAQCategories;
		}
		
		
		function getFAQsCategory($categoryIdentifier){
			
			$this->categoryIdentifier = $categoryIdentifier;
			$allFAQCategories = $this->getFAQCategories();
			$filteredFAQCategory = array_filter($allFAQCategories, array($this, 'filter_FAQsCategoryByCategoryId'));
			
			if (1 == count($filteredFAQCategory)) {
				$this->FAQsCategory = array_pop($filteredFAQCategory);
				return true;
			} else {
				return false;
			}
		}
		
		function filter_FAQsCategoryByCategoryId($o){
			if (is_null($this->categoryIdentifier)) {
				return false;
			} else if (is_numeric($this->categoryIdentifier)){
				return $this->categoryIdentifier == $o->documentcode;
			} else {
				return $this->categoryIdentifier == $o->title;
			}
		}
		
// render methods
		
		function renderFAQsCategoryNavigation(){
			$allFAQCategories = $this->getFAQCategories();
			
			
			
			if(is_null($this->renderFAQsCategoryFunction)) {
				global $FTAFAQCategoryItemRenderFunction;
				if(!isset($FTAFAQCategoryItemRenderFunction)){
					$FTAFAQCategoryItemRenderFunction = '$o=fta_getSharedFaqsObject(); echo $o->renderFAQsCategoryItemFunction($item);';
				}
	
				$this->renderFAQsCategoryFunction  = create_function('$item', $FTAFAQCategoryItemRenderFunction );
			}
			//we need to map the links and do some funky scope wrandling..
			fta_widgetList($allFAQCategories,"FAQsCategoryList","altItem","firstItem", "lastItem",$this->renderFAQsCategoryFunction);
			
		}
		
	
		function renderFAQsCategoryAsTagLine(){	

			if(is_null($this->FAQsCategory))
				return;				
			
			echo fta_widgetH5Tag($this->FAQsCategory->title);					
			
		}
		
		function renderFAQsCategoryAsSubHeading(){	
			
			if(is_null($this->FAQsCategory))
				return;				
			
			echo fta_widgetH2Tag($this->FAQsCategory->title);		
							
		}
		
		function renderFAQsCategoryAsMinorSubHeading(){
			if(is_null($this->FAQsCategory))
				return;				
			
			echo fta_widgetH3Tag($this->FAQsCategory->title);				
		}
		
		function renderFAQsCateogryAsHeading(){	
			if(is_null($this->FAQsCategory))
				return;				
			
			echo fta_widgetH1Tag($this->FAQsCategory->title);							
		}
		
		function renderFAQsCateogryDescription(){
			if(is_null($this->FAQsCategory))
				return;				
			
			echo $this->FAQsCategory->shortdescription;			
		}
		
		
		function renderFaqs($pageSize = null, $pageNumber = null, $category = null, $sort = null, $direction = null){ 
		
			$Faqs = $this->getFaqs($pageSize, $pageNumber, $category, $sort, $direction);
		
			if (0 == count($Faqs))	return;
			
			if(is_null($this->renderFaqsFunction)) {
				global $FTAFaqItemRenderFunction;
				if(!isset($FTAFaqItemRenderFunction)){
					$FTAFaqItemRenderFunction = '$o=fta_getSharedFaqsObject(); echo $o->renderFaqsItemFunction($item);';
				}
	
				$this->renderFaqsFunction  = create_function('$item', $FTAFaqItemRenderFunction );
			}
				
			//we need to map the Faqs and do some funky scope wrandling..
			fta_widgetList($Faqs,"FaqsList","altItem","firstItem", "lastItem",$this->renderFaqsFunction);
		}
		
		function renderFaqsItemFunction($item){
		//	//this is a fucking prick job.. screw over.. 
			?>
<div class="FaqsQuestion"><h3>Q: <?=$item->title ?></h3></div>

<div class="FaqsAnswer">A: <?= $item->shortdescription ?></div>
<?
		}
		
		function renderFAQsCategoryItemFunction($item){
			?>
			<div class="FaqsCategory"><h2><?php echo $item->title; ?></h2></div>
			<?
		}
	}
	
/////

	function fta_getFAQsCategoryIdentiferFromRequestOrDefault($defaultFAQsCategory=null){
		$categoryIdentifier = extractStringWithKeyFromArray('REQUEST_URI',$_SERVER,null);
		
		// we need to subtract the page scope from the passed in url
		global $fta_;
		$pageScope = $fta_->getPageScope();
		//match the string ... 
		//scope/xxx/yyy/
		if( preg_match("#^".$pageScope."(.*)/$#",$categoryIdentifier,$matches) ){
			$categoryIdentifier = urlFragmentCleanRestore($matches[1]);
		} else {
			$categoryIdentifier = $defaultFAQsCategory;
		}

		//actually we will just pull it from the request.. 
		
		
		$FAQsObject = fta_getSharedFaqsObject();
		if (!is_null($categoryIdentifier) && $FAQsObject->getFAQsCategory( $categoryIdentifier )){
			return $categoryIdentifier;
		} else if (!is_null($defaultFAQsCategory) && $FAQsObject->getFAQsCategory( $defaultFAQsCategory )){
			return $defaultFAQsCategory;
		} else {
			return null;
		}
	}
	
	
	function fta_getFAQsCategoryNavigation(){
		ob_start();
		$FAQsObject = fta_getSharedFAQsObject();
		$FAQsObject->renderFAQsCategoryNavigation();
		return ob_get_clean();
	}
	
	
	function fta_Faqs() {
		ob_start();
		$FaqsObject = fta_getSharedFaqsObject();
		$FaqsObject->renderFaqs();
		return ob_get_clean();		
	}
	
	
	function fta_getFAQsCategoryAsTagLine(){
		ob_start();
		$FAQsObject = fta_getSharedFAQsObject();
		$FAQsObject->renderFAQsCategoryAsTagLine();
		return ob_get_clean();			
	}
	
	function fta_getFAQsCategoryAsSubHeading(){
		ob_start();
		$FAQsObject = fta_getSharedFAQsObject();
		$FAQsObject->renderFAQsCategoryAsSubHeading();
		return ob_get_clean();			
	}
	
	
	function fta_getFAQsCategoryAsMinorHeading(){
		ob_start();
		$FAQsObject = fta_getSharedFAQsObject();
		$FAQsObject->renderFAQsCategoryAsMinorSubHeading();
		return ob_get_clean();			
	}	

	function fta_getFAQsCateogryAsHeading(){
		ob_start();
		$FAQsObject = fta_getSharedFAQsObject();
		$FAQsObject->renderFAQsCateogryAsHeading();
		return ob_get_clean();			
	}
	
	function fta_getFAQsCategoryDescription() {
		ob_start();
		$FAQsObject = fta_getSharedFAQsObject();
		$FAQsObject->renderFAQsCateogryDescription();
		return ob_get_clean();			
	}
	
	
	/**
	 * Gets the fta obejct.. these are singletons really..
	 *
	 * @return FTAWebPage
	 */
	function fta_getSharedFaqsObject(){

		global $fta_;

		global $FTAFaqsClass;
		
		if(!is_null($FTAFaqsClass) && "FTAFaqs" != $FTAFaqsClass){
		} else {
			$FTAFaqsClass = "FTAFaqs";
		}
			
		if (!array_key_exists('ftaFaqs',$fta_)){
			$fta_->ftaFaqs = new  $FTAFaqsClass();
		}
		return $fta_->ftaFaqs;
	}